package com.wangsh.splider.task.service.impl;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.wangsh.splider.common.pojo.ApiResponse;
import com.wangsh.splider.common.pojo.ApiResponseEnum;
import com.wangsh.splider.common.pojo.JSONEnum;
import com.wangsh.splider.common.service.BaseServiceImpl;
import com.wangsh.splider.common.util.ConstatFinalUtil;
import com.wangsh.splider.common.util.PageInfoUtil;
import com.wangsh.splider.task.dao.IATaskDescDao;
import com.wangsh.splider.task.dao.IATaskHistoryDao;
import com.wangsh.splider.task.dao.IATaskUsersDao;
import com.wangsh.splider.task.pojo.ATaskDesc;
import com.wangsh.splider.task.pojo.ATaskHistory;
import com.wangsh.splider.task.pojo.ATaskUsers;
import com.wangsh.splider.task.service.ITaskDbService;

/**
 * 数据模块的实现类
 * 
 * @author Wangsh
 * 
 */
@Service("taskDbService")
public class TaskDbServiceImpl extends BaseServiceImpl implements ITaskDbService {
	@Resource
	private IATaskUsersDao taskUsersDAO;

	@Override
	public ApiResponse<Object> saveOneUsersService(ATaskUsers users) {
		ApiResponse<Object> apiResponse = new ApiResponse<Object>();
		//users.setAge(users.getEdYear() - users.getStYear());

		int res = this.taskUsersDAO.save(users);
		if (res > 0) {
			Map<String, Object> dataMap = new HashMap<String, Object>();
			dataMap.put(JSONEnum.EFFECT.getName(), res);
			dataMap.put(JSONEnum.ID.getName(), users.getId());
			dataMap.put(ApiResponseEnum.NAME_ONE.getName(), users);
			apiResponse.setDataOne(dataMap);

			apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
		} else {
			apiResponse.setCode(ApiResponseEnum.STATUS_FAILED.getStatus());
		}
		apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""),
				Collections.EMPTY_MAP);
		return apiResponse;
	}
	
	@Override
	public ApiResponse<Object> saveBatchUsersService(List<ATaskUsers> usersList)
	{
		ApiResponse<Object> apiResponse = new ApiResponse<Object>();
		int res = this.taskUsersDAO.saveBatch(usersList);
		if (res > 0)
		{
			Map<String, Object> dataMap = new HashMap<String, Object>();
			dataMap.put(JSONEnum.EFFECT.getName(), res);
			
			apiResponse.setDataOne(dataMap);
			apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
		} else
		{
			apiResponse.setCode(ApiResponseEnum.STATUS_FAILED.getStatus());
		}
		apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""),
				Collections.EMPTY_MAP);
		return apiResponse;
	}

	@Override
	public ApiResponse<Object> updateOneUsersService(ATaskUsers users) {
		ApiResponse<Object> apiResponse = new ApiResponse<Object>();
			//users.setAge(users.getEdYear() - users.getStYear());

		int res = this.taskUsersDAO.update(users);
		if (res > 0) {
			Map<String, Object> dataMap = new HashMap<String, Object>();
			dataMap.put(JSONEnum.EFFECT.getName(), res);
			dataMap.put(JSONEnum.ID.getName(), users.getId());
			dataMap.put(ApiResponseEnum.NAME_ONE.getName(), users);
			apiResponse.setDataOne(dataMap);

			apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
		} else {
			apiResponse.setCode(ApiResponseEnum.STATUS_FAILED.getStatus());
		}
		apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""),
				Collections.EMPTY_MAP);
		return apiResponse;
	}
	
	@Override
	public ApiResponse<Object> updateBatchUsersService(List<ATaskUsers> usersList)
	{
		ApiResponse<Object> apiResponse = new ApiResponse<Object>();
		int res = this.taskUsersDAO.updateListBatch(usersList);
		if (res > 0)
		{
			Map<String, Object> dataMap = new HashMap<String, Object>();
			dataMap.put(JSONEnum.EFFECT.getName(), res);
			
			apiResponse.setDataOne(dataMap);
			apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
		} else
		{
			apiResponse.setCode(ApiResponseEnum.STATUS_FAILED.getStatus());
		}
		apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""),
					Collections.EMPTY_MAP);
		return apiResponse;
	}

	@Override
	public ApiResponse<Object> deleteOneUsersService(Map<String, Object> condMap) {
		ApiResponse<Object> apiResponse = new ApiResponse<Object>();
		int res = this.taskUsersDAO.delete(condMap);
		if (res > 0) {
			Map<String, Object> dataMap = new HashMap<String, Object>();
			dataMap.put(JSONEnum.EFFECT.getName(), res);
			apiResponse.setDataOne(dataMap);

			apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
		} else {
			apiResponse.setCode(ApiResponseEnum.STATUS_FAILED.getStatus());
		}
		apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""),
					Collections.EMPTY_MAP);
		return apiResponse;
	}

	@Override
	public ApiResponse<ATaskUsers> findCondListUsersService(PageInfoUtil pageInfoUtil, Map<String, Object> condMap) {
		ApiResponse<ATaskUsers> apiResponse = new ApiResponse<ATaskUsers>();
		apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
		List<ATaskUsers> demoUsersList = Collections.EMPTY_LIST;

		Map<String, List<ATaskUsers>> dataList = new HashMap<String, List<ATaskUsers>>();
		/* 对关键字进行模糊匹配 */
		if (condMap.get("keyword") != null && !"".equalsIgnoreCase(condMap.get("keyword") + "")) {
			condMap.put("keyword", "%" + condMap.get("keyword") + "%");
		}

		/* 设置分页相关信息 */
		if (pageInfoUtil != null) {
			/* 设置分页信息 */
			Page page = PageHelper.startPage(pageInfoUtil.getCurrentPage(), pageInfoUtil.getPageSize());
			demoUsersList = this.taskUsersDAO.findList(condMap);
			pageInfoUtil.setTotalRecord(Integer.valueOf(page.getTotal() + ""));
			/* 向接口中放分页信息 */
			apiResponse.setPageInfoUtil(pageInfoUtil);
		} else {
			demoUsersList = this.taskUsersDAO.findList(condMap);
		}
		/* 将结果放到JSON中 */
		dataList.put(ApiResponseEnum.NAME_LIST.getName(), demoUsersList);

		apiResponse.setDataListJava(demoUsersList);
		apiResponse.setDataList(dataList);
		apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""),
					Collections.EMPTY_MAP);
		return apiResponse;
	}

	@Override
	public ApiResponse<ATaskUsers> findOneUsersService(Map<String, Object> condMap) {
		ApiResponse<ATaskUsers> apiResponse = new ApiResponse<ATaskUsers>();

		Map<String, ATaskUsers> data = new HashMap<String, ATaskUsers>();
		ATaskUsers users = this.taskUsersDAO.findOne(condMap);
		data.put(ApiResponseEnum.NAME_ONE.getName(), users);
		apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
		/* 将结果放到JSON中 */
		apiResponse.setDataOne(data);
		apiResponse.setDataOneJava(users);
		apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""),
				Collections.EMPTY_MAP);
		return apiResponse;
	}
	@Resource
	private IATaskHistoryDao taskHistoryDao;
	
	@Override
	public ApiResponse<Object> saveOneHistoryService(ATaskHistory history)
	{
		ApiResponse<Object> apiResponse = new ApiResponse<Object>();
//			dynasty.setAge(dynasty.getEdYear() - dynasty.getStYear());
		
		int res = this.taskHistoryDao.save(history);
		if (res > 0)
		{
			Map<String, Object> dataMap = new HashMap<String, Object>();
			dataMap.put(JSONEnum.EFFECT.getName(), res);
			dataMap.put(JSONEnum.ID.getName(), history.getId());
			dataMap.put(ApiResponseEnum.NAME_ONE.getName(), history);
			apiResponse.setDataOne(dataMap);
			apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
		} else
		{
			apiResponse.setCode(ApiResponseEnum.STATUS_FAILED.getStatus());
		}
		apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
		return apiResponse;
	}

	@Override
	public ApiResponse<Object> updateOneHistoryService(ATaskHistory history)
	{
		ApiResponse<Object> apiResponse = new ApiResponse<Object>();
//			history.setAge(history.getEdYear() - history.getStYear());
		
		int res = this.taskHistoryDao.update(history);
		if (res > 0)
		{
			Map<String, Object> dataMap = new HashMap<String, Object>();
			dataMap.put(JSONEnum.EFFECT.getName(), res);
			dataMap.put(JSONEnum.ID.getName(), history.getId());
			dataMap.put(ApiResponseEnum.NAME_ONE.getName(), history);
			apiResponse.setDataOne(dataMap);
			
			apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
		} else
		{
			apiResponse.setCode(ApiResponseEnum.STATUS_FAILED.getStatus());
		}
		apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
		return apiResponse;
	}

	@Override
	public ApiResponse<Object> deleteOneHistoryService(Map<String, Object> condMap)
	{
		ApiResponse<Object> apiResponse = new ApiResponse<Object>();
		int res = this.taskHistoryDao.delete(condMap);
		if (res > 0)
		{
			Map<String, Object> dataMap = new HashMap<String, Object>();
			dataMap.put(JSONEnum.EFFECT.getName(), res);
			apiResponse.setDataOne(dataMap);
			
			apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
		} else
		{
			apiResponse.setCode(ApiResponseEnum.STATUS_FAILED.getStatus());
		}
		apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""),Collections.EMPTY_MAP);
		return apiResponse;
	}

	@Override
	public ApiResponse<ATaskHistory> findCondListHistoryService(PageInfoUtil pageInfoUtil , Map<String, Object> condMap)
	{
		ApiResponse<ATaskHistory> apiResponse = new ApiResponse<ATaskHistory>();
		apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
		List<ATaskHistory> aTaskHistoryList = Collections.EMPTY_LIST;
		
		Map<String, List<ATaskHistory>> dataList = new HashMap<String, List<ATaskHistory>>();
		/* 对关键字进行模糊匹配 */
		if(condMap.get("keyword") != null && !"".equalsIgnoreCase(condMap.get("keyword") + ""))
		{
			condMap.put("keyword", "%" + condMap.get("keyword") + "%") ; 
		}
		
		/* 设置分页相关信息 */
		if(pageInfoUtil != null)
		{
			/* 设置分页信息 */
			Page page = PageHelper.startPage(pageInfoUtil.getCurrentPage(),pageInfoUtil.getPageSize());
			aTaskHistoryList = this.taskHistoryDao.findList(condMap) ;
			pageInfoUtil.setTotalRecord(Integer.valueOf(page.getTotal() + ""));
			/* 向接口中放分页信息 */
			apiResponse.setPageInfoUtil(pageInfoUtil);
		}else
		{
			aTaskHistoryList = this.taskHistoryDao.findList(condMap) ;
		}
		/* 将结果放到JSON中 */
		dataList.put(ApiResponseEnum.NAME_LIST.getName(), aTaskHistoryList);
		
		apiResponse.setDataListJava(aTaskHistoryList);
		apiResponse.setDataList(dataList);
		apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""),Collections.EMPTY_MAP);
		return apiResponse; 
	}

	@Override
	public ApiResponse<ATaskHistory> findOneHistoryService(Map<String, Object> condMap)
	{
		ApiResponse<ATaskHistory> apiResponse = new ApiResponse<ATaskHistory>();
			
		Map<String, ATaskHistory> data = new HashMap<String, ATaskHistory>();
		ATaskHistory aTaskHistory = this.taskHistoryDao.findOne(condMap);
		data.put(ApiResponseEnum.NAME_ONE.getName(), aTaskHistory);
		apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
		/* 将结果放到JSON中 */
		apiResponse.setDataOne(data);
		apiResponse.setDataOneJava(aTaskHistory);
		apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""),Collections.EMPTY_MAP);
		return apiResponse ; 
	}

	/**
	 * 任务描述的Service实现类
	 *
	 * @author Songhy
	 */
	@Resource
	private IATaskDescDao taskDescDao;

	@Override
	public ApiResponse<Object> saveOneDescService(ATaskDesc taskDesc) {
		ApiResponse<Object> apiResponse = new ApiResponse<Object>();
		int res = this.taskDescDao.save(taskDesc);
		if (res > 0) {
			Map<String, Object> dataMap = new HashMap<String, Object>();
			dataMap.put(JSONEnum.EFFECT.getName(), res);
			dataMap.put(JSONEnum.ID.getName(), taskDesc.getId());
			dataMap.put(ApiResponseEnum.NAME_ONE.getName(), taskDesc);
			apiResponse.setDataOne(dataMap);

			apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
		} else {
			apiResponse.setCode(ApiResponseEnum.STATUS_FAILED.getStatus());
		}
		apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
		return apiResponse;
	}

	@Override
	public ApiResponse<Object> updateOneDescService(ATaskDesc taskDesc) {
		ApiResponse<Object> apiResponse = new ApiResponse<Object>();
		int res = this.taskDescDao.update(taskDesc);
		if (res > 0) {
			Map<String, Object> dataMap = new HashMap<String, Object>();
			dataMap.put(JSONEnum.EFFECT.getName(), res);
			dataMap.put(JSONEnum.ID.getName(), taskDesc.getId());
			dataMap.put(ApiResponseEnum.NAME_ONE.getName(), taskDesc);
			apiResponse.setDataOne(dataMap);

			apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
		} else {
			apiResponse.setCode(ApiResponseEnum.STATUS_FAILED.getStatus());
		}
		apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
		return apiResponse;
	}

	@Override
	public ApiResponse<Object> deleteOneDescService(Map<String, Object> condMap) {
		ApiResponse<Object> apiResponse = new ApiResponse<Object>();
		int res = this.taskDescDao.delete(condMap);
		if (res > 0) {
			Map<String, Object> dataMap = new HashMap<String, Object>();
			dataMap.put(JSONEnum.EFFECT.getName(), res);
			apiResponse.setDataOne(dataMap);

			apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
		} else {
			apiResponse.setCode(ApiResponseEnum.STATUS_FAILED.getStatus());
		}
		apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
		return apiResponse;
	}

	@Override
	public ApiResponse<ATaskDesc> findCondListDescService(PageInfoUtil pageInfoUtil, Map<String, Object> condMap) {
		ApiResponse<ATaskDesc> apiResponse = new ApiResponse<ATaskDesc>();
		apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
		List<ATaskDesc> taskDescList = Collections.EMPTY_LIST;

		Map<String, List<ATaskDesc>> dataList = new HashMap<String, List<ATaskDesc>>();
		/* 对关键字进行模糊匹配 */
		if (condMap.get("keyword") != null && !"".equalsIgnoreCase(condMap.get("keyword") + "")) {
			condMap.put("keyword", "%" + condMap.get("keyword") + "%");
		}

		/* 设置分页相关信息 */
		if (pageInfoUtil != null) {
			/* 设置分页信息 */
			Page page = PageHelper.startPage(pageInfoUtil.getCurrentPage(), pageInfoUtil.getPageSize());
			taskDescList = this.taskDescDao.findList(condMap);
			pageInfoUtil.setTotalRecord(Integer.valueOf(page.getTotal() + ""));
			/* 向接口中放分页信息 */
			apiResponse.setPageInfoUtil(pageInfoUtil);
		} else {
			taskDescList = this.taskDescDao.findList(condMap);
		}
		/* 将结果放到JSON中 */
		dataList.put(ApiResponseEnum.NAME_LIST.getName(), taskDescList);

		apiResponse.setDataListJava(taskDescList);
		apiResponse.setDataList(dataList);
		apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
		return apiResponse;
	}

	@Override
	public ApiResponse<ATaskDesc> findOneDescService(Map<String, Object> condMap) {
		ApiResponse<ATaskDesc> apiResponse = new ApiResponse<ATaskDesc>();

		Map<String, ATaskDesc> data = new HashMap<String, ATaskDesc>();
		ATaskDesc taskDesc = this.taskDescDao.findOne(condMap);
		data.put(ApiResponseEnum.NAME_ONE.getName(), taskDesc);
		apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
		/* 将结果放到JSON中 */
		apiResponse.setDataOne(data);
		apiResponse.setDataOneJava(taskDesc);
		apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
		return apiResponse;
	}
}